package com.joysw.ui.fragment;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.google.gson.Gson;
import com.joysw.R;
import com.joysw.models.GysModel;
import com.joysw.models.GysModelMsg;
import com.joysw.ui.FoodsDetailActivity;
import com.joysw.ui.ShopDetailActivity;
import com.joysw.view.CircularImageView;
import com.joysw.view.TitleBarView;
import com.nostra13.universalimageloader.core.ImageLoader;

/**   
 * @Title: GysFragment.java 
 * @Package com.joysw.ui.fragment 
 * @Description: 供应商
 * @author LIMINGYAN  
 * @date 2015年3月28日 下午9:28:29 
 * @version V1.0   
 */
public class GysFragment extends BaseFragment{
	private static final String TAG = "GysFragment";
	private Context mContext;
	private View mBaseView;
	
	@InjectView(R.id.gys_title_bar)
	TitleBarView mTitleBar;
	@InjectView(R.id.gys_list)
	ListView mListView;
	
	private MyListAdapter mListAdapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mContext = getActivity();
		mBaseView = inflater.inflate(R.layout.fragment_gys, container,
				false);
		ButterKnife.inject(this, mBaseView);
		initView();
		
		new MyTask().execute();
		return mBaseView;
	}
	
	private void initView(){
		mTitleBar.setCommonTitle(View.GONE, View.VISIBLE, View.VISIBLE);
		mTitleBar.setTitleText(R.string.fragment_gys_title);
		mTitleBar.setBtnRightText(R.string.fragment_gys_title_add);
		
		mListView.setOnItemClickListener(mListItemClickEvent);
	}
	
	class MyTask extends AsyncTask<Void, Void, GysModelMsg> {

		@Override
		protected GysModelMsg doInBackground(Void... params) {
			String json = "{\"code\":200,\"msg\":\"ok\",list:["
					+ "{\"id\":110,\"avator\":\"http://img0.bdstatic.com/img/image/shouye/leimu/mingxing.jpg\",\"name\":\"时尚衣服饰\"},"
					+ "{\"id\":110,\"avator\":\"http://img0.bdstatic.com/img/image/shouye/leimu/mingxing6.jpg\",\"name\":\"爱心店铺\"}"
					+"]}";
			Gson gson = new Gson();
			GysModelMsg msg = gson.fromJson(json, GysModelMsg.class);
			return msg;
		}

		@Override
		protected void onPostExecute(GysModelMsg result) {
			mListAdapter = new MyListAdapter(result.list);
			mListView.setAdapter(mListAdapter);
		}

	}
	
	private OnItemClickListener mListItemClickEvent=new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(GysFragment.this.getActivity(),ShopDetailActivity.class);
			startActivity(intent);
		}
	};
	
	public class MyListAdapter extends BaseAdapter{
		private ArrayList<GysModel> mList;
		private LayoutInflater mInflater;
		
		public MyListAdapter(ArrayList<GysModel> list){
			mInflater = LayoutInflater.from(getActivity());
			mList = list;
		}
		
		@Override
		public int getCount() {
			return mList==null?0:mList.size();
		}

		@Override
		public GysModel getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return getItem(position).id;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.list_item_gys, null);
				holder.avator=(CircularImageView)convertView.findViewById(R.id.gys_img);
				holder.name=(TextView)convertView.findViewById(R.id.gys_name);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			final GysModel bean = getItem(position);
			ImageLoader.getInstance().displayImage(bean.avator, holder.avator);
			holder.name.setText(bean.name);
			return convertView;
		}
	}
	
	private static class ViewHolder {
		public TextView name;//店铺名
		public ImageView avator;//店铺头像
	}
}
